
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品详情页</title>
    <style type="text/css">
    .empty {
        text-align: center;
        padding: 5px 0;
    }
    .top{
    	width:100%;
    	height: 40px;
    	margin: 40px auto 0px;
    	background: #f5f5f5;
    	color:#000;
    	border:0px;
    	display: block;
    	text-align: center;
    }
    .leftreturn{
      position: absolute;
      left:0;
      z-index: 5;
      background-image: url("../../image/leftreturn.png");
      background-position: 0px 0px;
      width:25px;
      height:25px;
      margin:7.5px 0;
    }
    .top-title{
      line-height: 40px;
      font-size: 20px;
    }
.content{
	width:100%;
	height: 500px;
	margin: 0 auto;
	background: white;
	color:#000000;
	border:0px;
	display: block;
	font-size: 14px;
}
.content-xinxi{
	width:100%;
	height: 500px;
	font-size: 18px;
	margin: 10px auto 0px;
}
.picture{
	width: 200px;
	height: 200px;
	margin: 10px auto 0px;
}
.picture img{
	width: 200px;
	height: 200px;
}
.content-text{
	width: 90%;
	height: 30px;
	margin:  auto;
	color:#000000;
	border:1px;
	border-style: solid;
	border-color: #999999;
	display: block;
	font-size: 14px;
}
.button {
	width:80%;
	height: 40px;
	margin: 0 auto;
	margin-top: 35px;
	background: #0082E4;
	color:#FFFFFF;
	font-size: 18px;
	border:0px;
	display: block;
}
    </style>
</head>
<body>
  <div class="empty">
    	<div class="top">
        <i class="leftreturn"></i>
    		<h1 class="top-title">商品详情页</h1>
    	</div>
    	<div class="content" id="content">
        <script type="text/x-dot-template"id="tmpl-5">
    		<div class="content-xinxi" >
    			<div class="picture">
                    <a href="#"><img src="{{=it[0].cimage.url}}"></a>
                </div>
    			<div class="content-xinxi">
    			<label for="">名称：</label><label for="">{{=it[0].cname}}</label><br>
          {{? it[0].cprice!=null}}
    			<label for="">价格：</label><label for="">￥{{=it[0].cprice}}</label><br>
          {{??}}
          <label for="">价格：</label><label for="">￥0</label><br>
          {{?}}
          {{? it[0].postage!=null}}
    			<label for="">邮费：</label><label for="">￥{{=it[0].postage}}</label><br>
          {{??}}
    			<label for="">邮费：</label><label for="">￥0</label><br>
          {{?}}
          {{? it[0].amount!=null}}
          <label for="">销量：</label><label for="">{{=it[0].amount}}件</label><br>
          {{??}}
    			<label for="">销量：</label><label for="">0 件</label><br>
          {{?}}
    			<label for="">详情：</label><label for="">{{=it[0].introduce}}</label>
    			<button class="button" onclick=(addToShopingCart())>加 入 购 物 车</button>
          </script>
    			</div>
    		</div>
      </script>
    	</div>
  </div>

</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/doT.min.js"></script>
<script type="text/javascript" src="../../script/SHA1.js"></script>
<script type="text/javascript">
apiready = function() {
    db = api.require('db');
    // 获取商品详情数据
    getWareInfo(api.pageParam.wareId);
    userInfor=$api.getStorage('userInfor');

};

//从数据库获得商品信息

function getWareInfo(wareId){

  var params = {
      fields: {},
      where: {
          id: wareId
      },
      skip: 0,
      limit: 1,
  }

      //获取X-APICloud-AppKey值
      var now = Date.now();
      appKey = SHA1("A6032422409194"+"UZ"+"808CF36B-60D4-DC5D-C9F3-FB49F9E1FB44"+"UZ"+now)+"."+now
      params = $api.jsonToStr(params);

     api.ajax({
         url: 'http://d.apicloud.com/mcm/api/commodity?filter=' + params,
         method: 'get',
         cache: false,
         headers:{
           "X-APICloud-AppId": "A6032422409194",
           "X-APICloud-AppKey": appKey
         }
     },function(ret, err){
         if (ret) {
            //  alert( JSON.stringify( ret ) );

             //使用doT模板将用户信息放入html中
             // 编译模板函数
             tmpltxt=doT.template(document.getElementById("tmpl-5").innerHTML);//生成模板方法
             document.getElementById("content").innerHTML=tmpltxt(ret);//数据渲染
         } else {
             alert('数据库连接失败');
         }
     });


}

function addToShopingCart(){
  //将商品id放入db数据库

  db.selectSql({
    name: userInfor.id,
    sql: "SELECT * From goods WHERE goodId ='"+api.pageParam.wareId+"'"
  }, function(ret, err){
  if( ret.data!=''){
               alert('商品已存在于购物车');
               api.openWin({
                   name: 'shoppingcart',
                   url: '../my/shoppingcart.html',
               });
  }else if(ret.data==''){
            //添加商品至数据库
           db.executeSql({
               name: userInfor.id,
               sql: "INSERT INTO goods (goodId,goodCount) VALUES ('"+api.pageParam.wareId+"',"+1+")"
           }, function(ret, err){
               if( ret.status ){
                   alert('商品已添加至购物车');
                   api.openWin({
                       name: 'index',
                       url: '../my/shoppingcart.html'
                   });
               }
           });
       }else if(err){
              alert("数据库连接失败")
            }
  });


}


</script>
</html>
